<?php

define('IN_ECS', true);
require(dirname(__FILE__) . '/includes/init.php');
$smarty->assign('helps',           get_shop_help());       // 帮助
assign_template();
$act = isset($_REQUEST['act']) ? $_REQUEST['act']  : '';

if($act==''){
    /*获取产品分类*/
    $sql="select cat_name, cat_id from".$ecs->table('category')." where parent_id=0 AND cat_name != '心愿' AND cat_name != '定制化'";
    $category_list = $db->getAll($sql);
    foreach($category_list as $key=>$vo)
    {
        //echo $vo['cat_id'];
        $children = get_children($vo['cat_id']);
        $category_list[$key]['goods'] = category_get_goods('', $children);

    }
    $position = assign_ur_here();
    $smarty->assign('page_title',       $position['title']);    // 页面标题
    $smarty->assign('ur_here',          $position['ur_here']);  // 当前位置
    $smarty->assign('category_list',       $category_list);
//    assign_pager('comparison',            $cat_id, $count, $size, $sort, $order, $page, '', $brand, $price_min, $price_max, $display, $filter_attr_str); // 分页
    $smarty->display('comparison.dwt');
}
elseif($act=='show'){
    $id=$_REQUEST['id'];
    /*获取本店对比产品的信息*/
    $sql = "select goods_img, goods_brief, goods_name, goods_id ,shop_price from ".$ecs->table('goods')." where goods_id=".$id;
    $goods_info = $db->getRow($sql);
    $goods_info['url'] = build_uri('goods', array('gid'=>$goods_info['goods_id']), $goods_info['goods_name']);
    $goods_info['sell_count']  = selled_count($goods_info['goods_id']); //添加 销售量
    $smarty->assign('goods_info',$goods_info);

    /*获取和本店对比的产品的品牌*/
    $goods_goods_id = $goods_info["goods_id"];
    $sql = "select c.b_brand_id, c.b_id ,cb.brand_name ,a_brand_id from ".$ecs->table('comparison')." as c left join ".$ecs->table('comparison_brand')." as cb on c.b_brand_id=cb.brand_id where a_id='$goods_goods_id'". " GROUP BY c.b_brand_id";
    $brand_list = $db->getAll($sql);
    if ($brand_list) {
        $a_brand_id=$brand_list['0']['a_brand_id'];
        foreach($brand_list as $key=>$vo){
            $children = $vo['b_brand_id'];
            $sql = "select g.goods_name, g.goods_img ,g.goods_id from ".$ecs->table('comparison')." as c left join ".$ecs->table('comparison_goods')." as g on c.b_id=g.goods_id left join ".$ecs->table('comparison_brand')." as b on c.b_brand_id=b.brand_id where c.b_id=g.goods_id AND c.b_brand_id=".$children." AND c.a_brand_id=".$a_brand_id;
            $brand_list[$key]['goods'] = $db->getAll($sql);
        }
    }
    
    $smarty->assign('brand_list',$brand_list);
    $position = assign_ur_here();
    $smarty->assign('page_title',       $position['title']);    // 页面标题
    $smarty->assign('ur_here',          $position['ur_here']);  // 当前位置

    $smarty->display('comparison_show.dwt');
}
elseif($act=='change_brand'){
    $sql = "select c.comparison_id, g.goods_name, g.goods_img, b.brand_name from ".$ecs->table('comparison')." as c left join ".$ecs->table('comparison_goods')." as g on c.b_id=g.goods_id left join ".$ecs->table('comparison_brand')." as b on c.b_brand_id=b.brand_id where c.a_id=".$_REQUEST['goods_id']." and c.b_brand_id=".$_REQUEST['brand_id'];
    $comparison_list = $db->getAll($sql);
    $res = '';
    foreach($comparison_list as $val){
        $res .="<div style='float:left'>"."<a href='comparison.php?act=content&id=".$val['comparison_id']."'><img src='".$val['goods_img']."' style='width:150px;height:150px;'></a><br/><a href='comparison.php?act=content&id=".$val['comparison_id']."'><strong>".$val['brand_name']."-".$val['goods_name']."</strong></a></div>";
    }
    echo $res;
}
elseif($act=='content'){
    $id=$_REQUEST['id'];
    $sql = "select comparison_name, a_id, a_brand_id, b_id, b_brand_id, ab_desc from ".$ecs->table('comparison')." where b_id=".$id;
    $arr = $db->getRow($sql);

    $smarty->assign('desc',$arr['ab_desc']);

    $sql = "select goods_img, goods_name, goods_id ,shop_price from ".$ecs->table('goods')." where goods_id=".$arr['a_id'];
    $goods_a = $db->getRow($sql);
    $goods_a['url'] = build_uri('goods', array('gid'=>$goods_a['goods_id']), $goods_a['goods_name']);
    $goods_a['sell_count']  = selled_count($goods_a['goods_id']); //添加 销售量
    $smarty->assign('goods_a',$goods_a);

    $sql = "select g.goods_name, g.goods_img, b.brand_name from ".$ecs->table('comparison_goods')." as g left join ".$ecs->table('comparison_brand')." as b on g.brand_id=b.brand_id where g.goods_id=".$arr['b_id'];
    $goods_b = $db->getRow($sql);
    $smarty->assign('goods_b',$goods_b);

    $smarty->assign('best_goods',      get_recommend_goods('best'));    // 推荐商品

    $position = assign_ur_here();
    $smarty->assign('page_title',       $position['title']);    // 页面标题
    $smarty->assign('ur_here',          $position['ur_here']);  // 当前位置
    $smarty->display('comparison_content.dwt');
}
/**
 * 获得分类下的商品总数
 *
 * @access  public
 * @param   string     $cat_id
 * @return  integer
 */
function get_cagtegory_goods_count( $min = 0, $max = 0)
{
    $where  = "g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0 ";
    if ($min > 0)
    {
        $where .= " AND g.shop_price >= $min ";
    }
    if ($max > 0)
    {
        $where .= " AND g.shop_price <= $max ";
    }

    $goods_id = $GLOBALS['db']->getAll('SELECT distinct c.a_id FROM ' . $GLOBALS['ecs']->table('comparison') . ' as c left join ' . $GLOBALS['ecs']->table('goods') . " AS g on c.a_id = g.goods_id WHERE $where");

    /* 返回商品总数 */
    return count($goods_id);
}
/**
 * 获得分类下的商品
 *
 * @access  public
 * @param   string  $children
 * @return  array
 */
function category_get_goods($type = '', $cats = '', $min = '', $max = '', $size = 100, $page = 1, $sort = '', $order = '')
{
    $where = "g.is_on_sale = 1 AND g.is_alone_sale = 1 AND ".
        "g.is_delete = 0 ";
    if ($min > 0)
    {
        $where .= " AND g.shop_price >= $min ";
    }
    if ($max > 0)
    {
        $where .= " AND g.shop_price <= $max ";
    }

    switch ($type)
    {
        case 'best':
            $where .= ' AND g.is_best = 1';
            break;
        case 'new':
            $where .= ' AND g.is_new = 1';
            break;
        case 'hot':
            $where .= ' AND g.is_hot = 1';
            break;
        case 'promote':
            $time = gmtime();
            $where .= " AND g.is_promote = 1 AND g.promote_start_date <= '$time' AND g.promote_end_date >= '$time'";
            break;
    }

    if (!empty($cats))
    {
        $where .= " AND (" . $cats . " OR " . get_extension_goods($cats) .")";
    }
    if(empty($order)){
        $order = 'desc';
    }
    if(empty($sort)){
        $order_type = $GLOBALS['_CFG']['recommend_order'];
        $sort = ($order_type == 0) ? " ORDER BY g.sort_order $order, g.last_update $order " : " ORDER BY RAND()";
    }

    /* 获得商品列表 */
    $sql = 'SELECT g.goods_id, g.goods_name, g.goods_name_style, g.market_price, g.is_new, g.is_best, g.is_hot, g.shop_price AS org_price, ' .
        "IFNULL(mp.user_price, g.shop_price * '$_SESSION[discount]') AS shop_price, g.promote_price, g.goods_type, " .
        'g.promote_start_date, g.promote_end_date, g.goods_brief, g.goods_thumb , g.goods_img ' .
        'FROM ' . $GLOBALS['ecs']->table('category') . ' AS c ' .
        'LEFT JOIN ' . $GLOBALS['ecs']->table('goods') . ' AS g ' .
        'ON c.cat_id = g.cat_id '.
        'LEFT JOIN ' . $GLOBALS['ecs']->table('member_price') . ' AS mp ' .
        "ON mp.goods_id = g.goods_id AND mp.user_rank = '$_SESSION[user_rank]' " .
        " WHERE $where $sort ";

    $res = $GLOBALS['db']->selectLimit($sql, $size, ($page - 1) * $size);
    $arr = array();
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
        if ($row['promote_price'] > 0)
        {
            $promote_price = bargain_price($row['promote_price'], $row['promote_start_date'], $row['promote_end_date']);
        }
        else
        {
            $promote_price = 0;
        }

        /* 处理商品水印图片 */
        $watermark_img = '';
        if ($promote_price != 0)
        {
            $watermark_img = "watermark_promote_small";
        }
        elseif ($row['is_new'] != 0)
        {
            $watermark_img = "watermark_new_small";
        }
        elseif ($row['is_best'] != 0)
        {
            $watermark_img = "watermark_best_small";
        }
        elseif ($row['is_hot'] != 0)
        {
            $watermark_img = 'watermark_hot_small';
        }

        if ($watermark_img != '')
        {
            $arr[$row['goods_id']]['watermark_img'] =  $watermark_img;
        }

        $arr[$row['goods_id']]['goods_id']         = $row['goods_id'];
        $arr[$row['goods_id']]['goods_name']       = $row['goods_name'];
        $arr[$row['goods_id']]['name']             = $row['goods_name'];
        $arr[$row['goods_id']]['goods_brief']      = $row['goods_brief'];
        $arr[$row['goods_id']]['goods_style_name'] = add_style($row['goods_name'],$row['goods_name_style']);
        $arr[$row['goods_id']]['market_price']     = price_format($row['market_price']);
        $arr[$row['goods_id']]['shop_price']       = price_format($row['shop_price']);
        $arr[$row['goods_id']]['type']             = $row['goods_type'];
        $arr[$row['goods_id']]['promote_price']    = ($promote_price > 0) ? price_format($promote_price) : '';
        $arr[$row['goods_id']]['goods_thumb']      = get_image_path($row['goods_id'], $row['goods_thumb'], true);
        $arr[$row['goods_id']]['goods_img']        = get_image_path($row['goods_id'], $row['goods_img']);
        $arr[$row['goods_id']]['url']              = build_uri('goods', array('gid'=>$row['goods_id']), $row['goods_name']);
    }
    return $arr;
}


?>